//
//  LGRefundBalanceView.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2025/6/24.
//

#import "LGRefundBalanceView.h"
#import "LGCustomTextView.h"

@interface LGRefundBalanceView()
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UILabel *refundAmount;
/**   */
@property (nonatomic , strong)UIButton *amountTitle;
/**   */
@property (nonatomic , strong)UITextField *amountTF;
/**   */
@property (nonatomic , strong)UILabel *amountUnit;
/**   */
@property (nonatomic , strong)UILabel *reasonTitle;
/**   */
@property (nonatomic , strong)LGCustomTextView *reasonView;
/**   */
@property (nonatomic , strong)UIButton *cancelBtn;
/**   */
@property (nonatomic , strong)UIButton *sureButton;

@end

@implementation LGRefundBalanceView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubView];
        CGFloat height = kMaxY(self.sureButton.frame)+viewPix(10);
        [UIView animateWithDuration:0.4 animations:^{
            self.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0.8];
            self.baseView.frame = CGRectMake(0, Screen_H-height, Screen_W, self.baseView.bounds.size.height);
        }];
        [self.amountTF becomeFirstResponder];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    }
    return self;
}


-(void)setAmount:(NSString *)amount{
    _amount = amount;
    self.refundAmount.text = [NSString stringWithFormat:@"可退款金额：%@元",amount];
}

-(void)keyboardWillShow:(NSNotification *)notification{
    NSInteger duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] integerValue];
    CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat height = keyboardFrame.size.height;
    height = height+kMaxY(self.sureButton.frame)+viewPix(10);
    [UIView animateWithDuration:duration animations:^{
        self.baseView.frame = CGRectMake(0, Screen_H-height, Screen_W, self.baseView.bounds.size.height);
    }];
}

//-(void)keyboardWillHide:(NSNotification *)notification{
//    NSInteger duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] integerValue];
//    [UIView animateWithDuration:duration animations:^{
//        self.baseView.frame = CGRectMake(0, Screen_H-self.baseView.bounds.size.height+viewPix(20), Screen_W, self.baseView.bounds.size.height);
//    } completion:^(BOOL finished) {
//        [self removeFromSuperview];
//    }];
//}

-(void)cancelButtonAction{
    [self.amountTF resignFirstResponder];
    [self.reasonView resignFirstResponder];
    [UIView animateWithDuration:0.4 animations:^{
        self.baseView.frame = CGRectMake(0, Screen_H, Screen_W, self.baseView.bounds.size.height);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

-(void)sureBtnAction{
    NSString *amount = LGNSString(self.amountTF.text);
    if ([amount isEmptyString]) {
        [TooltipView showMessage:@"请输入退款金额" offset:0];
        return;
    }
    
    if ([amount floatValue] > [self.amount floatValue]) {
        [TooltipView showMessage:@"退款总额超出已支付总金额" offset:0];
        return;
    }
    
    if (self.confirmRefundAction) {
        self.confirmRefundAction(amount, LGNSString(self.reasonView.content));
    }
    [self cancelButtonAction];
}


#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    self.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0];
    [self addSubview:self.baseView];
    [self.baseView addSubview:self.titleLabel];
    [self.baseView addSubview:self.refundAmount];
    [self.baseView addSubview:self.amountTitle];
    [self.baseView addSubview:self.amountTF];
    [self.baseView addSubview:self.amountUnit];
    [self.baseView addSubview:self.reasonTitle];
    [self.baseView addSubview:self.reasonView];
    [self.baseView addSubview:self.cancelBtn];
    [self.baseView addSubview:self.sureButton];
}

-(UIView *)baseView{
    if(!_baseView){
        _baseView = [[UIView alloc]initWithFrame:CGRectMake(0, Screen_H, Screen_W, Screen_H)];
        _baseView.backgroundColor = [UIColor whiteColor];
        _baseView.cornerRidus = 15;
    }
    return _baseView;
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"退款" colorString:@"#333333" textFont:LGFontWeight(18, UIFontWeightMedium) textAlignment:NSTextAlignmentCenter lines:1];
        _titleLabel.frame = CGRectMake(0, viewPix(15), Screen_W, viewPix(22));
    }
    return _titleLabel;
}

-(UILabel *)refundAmount{
    if(!_refundAmount){
        _refundAmount = [UILabel lableWithText:@"可退款金额：" colorString:@"#333333" textFont:LGFont(15) textAlignment:NSTextAlignmentLeft lines:1];
        _refundAmount.frame = CGRectMake(viewPix(20), kMaxY(self.titleLabel.frame)+viewPix(10), Screen_W, viewPix(20));
    }
    return _refundAmount;
}

-(UIButton *)amountTitle{
    if(!_amountTitle){
        _amountTitle = [UIButton buttonWithTitle:@" 本次退款金额" titleFont:LGFont(15) textColor:@"#333333" imageName:@"必选项2"];
        _amountTitle.frame = CGRectMake(viewPix(20), kMaxY(self.refundAmount.frame)+viewPix(10), viewPix(105), viewPix(40));
        _amountTitle.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _amountTitle.userInteractionEnabled = NO;
    }
    return _amountTitle;
}

-(UITextField *)amountTF{
    if(!_amountTF){
        _amountTF = [[UITextField alloc]initWithFrame:CGRectMake(viewPix(130), kMinY(self.amountTitle.frame), Screen_W-viewPix(170), viewPix(40))];
        [_amountTF addBorder:@"#E3E3E3" lineWidth:1 cornerRidus:5];
        _amountTF.tintColor = [UIColor colorWithString:@"#333333"];
        _amountTF.textColor = [UIColor colorWithString:@"#333333"];
        _amountTF.textAlignment = NSTextAlignmentLeft;
        _amountTF.keyboardType = UIKeyboardTypeDecimalPad;
        _amountTF.placeholder = @"请输入退款金额";
        _amountTF.font = LGFont(15);
        _amountTF.leftViewMode = UITextFieldViewModeAlways;
        _amountTF.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, viewPix(5), viewPix(40))];
    }
    return _amountTF;
}

-(UILabel *)amountUnit{
    if(!_amountUnit){
        _amountUnit = [UILabel lableWithText:@"元" colorString:@"#666666" textFont:LGFont(15) textAlignment:NSTextAlignmentLeft lines:1];
        _amountUnit.frame = CGRectMake(kMaxX(self.amountTF.frame)+viewPix(5), kMinY(self.amountTF.frame), viewPix(25), viewPix(40));
    }
    return _amountUnit;
}

-(UILabel *)reasonTitle{
    if(!_reasonTitle){
        _reasonTitle = [UILabel lableWithText:@"退款原因" colorString:@"#333333" textFont:LGFont(15) textAlignment:NSTextAlignmentLeft lines:1];
        _reasonTitle.frame = CGRectMake(viewPix(20), kMaxY(self.amountTitle.frame)+viewPix(10), viewPix(100), viewPix(30));
    }
    return _reasonTitle;
}

-(LGCustomTextView *)reasonView{
    if(!_reasonView){
        _reasonView = [LGCustomTextView textViewWithPlaceHolder:@"请输入退款原因" textColor:@"#333333" textFont:LGFont(15) textAlignment:NSTextAlignmentLeft keyboard:UIKeyboardTypeDefault];
        _reasonView.frame = CGRectMake(viewPix(130), kMinY(self.reasonTitle.frame), Screen_W-viewPix(150), viewPix(150));
        _reasonView.backgroundColor = [UIColor colorWithString:@"#FFFFFF"];
        [_reasonView addBorder:@"#E3E3E3" lineWidth:1 cornerRidus:5];
        _reasonView.placeHolderColor = @"#CCCCCC";
        _reasonView.placeHolderFont = LGFont(15);
        _reasonView.maxCount = 500;
    }
    return _reasonView;
}

-(UIButton *)cancelBtn{
    if(!_cancelBtn){
        _cancelBtn = [UIButton buttonWithTitle:@"取消" titleFont:LGFontWeight(16, UIFontWeightMedium) normalColor:@"#429BFF" selectColor:@"#429BFF" frame:CGRectMake(viewPix(20), kMaxY(self.reasonView.frame)+viewPix(25), (Screen_W-viewPix(60))/2.0, viewPix(40))];
        [_cancelBtn addBorder:@"#429BFF" lineWidth:1 cornerRidus:5];
        [_cancelBtn addTarget:self action:@selector(cancelButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelBtn;
}

-(UIButton *)sureButton{
    if(!_sureButton){
        _sureButton = [UIButton buttonWithBackColor:@"#429BFF" cornerRadius:5 title:@"确定" titleFont:LGFontWeight(16, UIFontWeightMedium) textColor:@"#FFFFFF" frame:CGRectMake(kMaxX(self.cancelBtn.frame)+viewPix(20), kMinY(self.cancelBtn.frame), (Screen_W-viewPix(60))/2.0, viewPix(40))];
        [_sureButton addTarget:self action:@selector(sureBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sureButton;
}


@end
